package com.javafactroy.kunlong.client.feign.factroy;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class ApiGeneralFactroy<T> {

    @Autowired
    List<T> list;

    public <E extends T> E find(Boolean remote) {
        for (T o : list) {
            String typeName = o.getClass().getAnnotatedInterfaces()[0].getType().getTypeName();
            if (remote && typeName.contains("Remote")) {
                return (E) o;
            }
            if (!remote && typeName.contains("Local")) {
                return (E) o;
            }

        }
        return null;
    }

    public <E extends T> E find(Class<E> cls) {
        for (T o : list) {
            if (o.getClass().getSimpleName().equalsIgnoreCase(cls.getSimpleName())) {
                return (E) o;
            }
            String oTypeName = o.getClass().getAnnotatedInterfaces()[0].getType().getTypeName();

            if ( oTypeName.equalsIgnoreCase(cls.getTypeName())) {
                return (E) o;
            }
        }
        return null;
    }

    public <E extends T> E findByIface(Class<E> cls) {

        for (T o : list) {
            String typeName = o.getClass().getAnnotatedInterfaces()[0].getType().getTypeName();

            if ( typeName.equalsIgnoreCase(cls.getTypeName())) {
                return (E) o;
            }

        }
        return null;
    }
}
